package com.xizi.秋招笔试;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * @author xizizzz
 * @description: TODO
 * @date 2021-8-22下午 03:51
 */
public class iqiyi2 {

    public static void main(String[] args) {
        final Scanner sc = new Scanner(System.in);
        String s = sc.nextLine();
        String[] str = s.split("=");
        int n = Integer.parseInt(str[1]);
        if (n == 2 || n == 3) {
            System.out.println(new ArrayList());
        }
        final List<List<String>> lists = solveNQueens(n);
        System.out.println(lists.toString());

    }

    public static List<List<String>> solveNQueens(int n) {
        ////回溯
        char[][] chess = new char[n][n];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                chess[i][j] = '.';
            }
        }
        List<List<String>> res = new ArrayList<>();
        solve(res, chess, 0);
        return res;
    }

    public static void solve(List<List<String>> res, char[][] chess, int row) {
        if (row == chess.length) {
            res.add(construct(chess));
            return;
        }
        for (int col = 0; col < chess[0].length; col++) {
            //对与改行的每一列进行判断
            if (valid(chess, row, col)) {
                chess[row][col] = 'Q';
                //行数+1
                solve(res, chess, row + 1);
                ////回溯条件 重置为.
                chess[row][col] = '.';
            }
        }
    }

    //数组转换为list
    public static List<String> construct(char[][] chess) {
        List<String> path = new ArrayList<>();
        for (int i = 0; i < chess.length; i++) {
            path.add(new String(chess[i]));
        }
//            System.out.println(path.toString());
        return path;
    }

    public static boolean valid(char[][] chess, int row, int col) {
        //列上判断
        for (int i = 0; i < row; i++) {
            if (chess[i][col] == 'Q') {
                return false;
            }
        }

        // /判断右上角
        for (int i = row - 1, j = col + 1; i >= 0 && j < chess.length; i--, j++) {
            if (chess[i][j] == 'Q') {
                return false;
            }
        }
        //判断左上角
        for (int i = row - 1, j = col - 1; i >= 0 && j >= 0; i--, j--) {
            if (chess[i][j] == 'Q') {
                return false;
            }
        }
        return true;
    }
}
